﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using BusinessLogic;
using BusinessObjects;
using DCMS_Framework;
using DCMSExceptions;
namespace Presentation.Children {

  /// <summary>
  /// ChildFiche UserControl
  /// </summary>
  public partial class ChildFicheControl : UserControl {

    private BindingSource controlBindingSource;
    private Child currentChild;

    #region - Properties -

    /// <summary>
    /// Gets or sets the current child.
    /// </summary>
    /// <value>
    /// The current child.
    /// </value>
    public Child CurrentChild {
      get { return currentChild; }
      set { currentChild = value; }
    }

    /// <summary>
    /// Gets or sets thebindingsource for control .
    /// </summary>
    /// <value>
    /// The controls bindingsource.
    /// </value>
    public BindingSource ControlBindingSource {
      get { return controlBindingSource; }
      set { controlBindingSource = value; }
    }

    #endregion

    #region - Constructor -

    /// <summary>
    /// Initializes a new instance of the <see cref="ChildFicheControl"/> class.
    /// </summary>
    public ChildFicheControl( ) {
      InitializeComponent( );
      trustPersonGridView.AutoGenerateColumns = false;

      controlBindingSource = new BindingSource( );
      controlBindingSource.DataSource = typeof( Child );
      openPictureFileDialog.Filter =
        "jpg files (*.jpg)|*.jpg|png files (*.png)|*.png|bmp files|*.bmp|All files (*.*)|*.*";

    }

    #endregion

    #region - Events -

    private void ChildFicheControl_Load( object sender, EventArgs e ) {
      SetDataBinding( );
    }

    private void childPhotoButton_Click( object sender, EventArgs e ) {

      if ( openPictureFileDialog.ShowDialog( ) == DialogResult.OK ) {
        Image photo = Image.FromFile( openPictureFileDialog.FileName );
        currentChild.Photo = photo;
      }

    }

    private void childPhotoPictureBox_Paint( object sender, PaintEventArgs e ) {
      if ( currentChild != null ) {
        pictureCloseButton.Visible = ( currentChild.Photo != null );
      }
    }

    private void pictureCloseButton_Click( object sender, EventArgs e ) {
      if ( MessageBox.Show( "Foto verwijderen?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.Yes ) {
        currentChild.Photo = null;
      }

    }

    private void trustPersonGridView_CellDoubleClick( object sender, DataGridViewCellEventArgs e ) {
      ShowEditTrustPersons( );
    }

    private void editTrustpersonButton_Click( object sender, EventArgs e ) {
      ShowEditTrustPersons( );
    }

    private void addTrustPersonButton_Click( object sender, EventArgs e ) {
      TrustPerson person = new TrustPerson( );
      person.ChildId = currentChild.Id;
      TrustPersons list = new TrustPersons( );
      list.Add( person );
      if ( currentChild.IsNew ) {
        if ( MessageBox.Show( "Kind bewaren?", "Nieuw kind", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == System.Windows.Forms.DialogResult.Yes ) {
          try {
            new ChildrenBll( ).SaveChild( currentChild );

            TrustPersonForm form = new TrustPersonForm( list, 0 );
            form.ShowDialog( );
            foreach ( TrustPerson newPerson in form.TrustPersonList ) {
              if ( newPerson.Id != 0 ) {
                currentChild.TrustPersons.Add( newPerson.Copy( ) );
              }
            }
          } catch ( BusinessException ex ) {
            MessageBox.Show( ex.Message,"Oops",MessageBoxButtons.OK,MessageBoxIcon.Error );
          }


        }
      } else {

        TrustPersonForm form = new TrustPersonForm( list, 0 );
        form.ShowDialog( );
        foreach ( TrustPerson newPerson in form.TrustPersonList ) {
          if ( newPerson.Id != 0 ) {
            currentChild.TrustPersons.Add( newPerson.Copy( ) );
          }
        }
      }
      trustPersonBindingSource.DataSource = null;
      trustPersonBindingSource.DataSource = currentChild.TrustPersons;
    }

    private void removeTrustPersonButton_Click( object sender, EventArgs e ) {
      if ( trustPersonGridView.SelectedRows.Count > 0 ) {
        if ( trustPersonGridView.SelectedRows[ 0 ].DataBoundItem != null ) {
          if ( MessageBox.Show( "Vertrouwenspersoon verwijderen?", "Verwijderen vertrouwenspersoon", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == System.Windows.Forms.DialogResult.Yes ) {

            new TrustPersonBll( ).DeleteTrustPerson( ( TrustPerson ) trustPersonGridView.SelectedRows[ 0 ].DataBoundItem );
            trustPersonBindingSource.RemoveCurrent( );
          }
        }
      }
    }

    private void linkButton_Click( object sender, EventArgs e ) {
      ChildrenListForm listForm = new ChildrenListForm( );
      if ( listForm.ShowDialog( ) == System.Windows.Forms.DialogResult.OK ) {
        childLinkTextBox.Text = listForm.SelectedChild.Id.ToString( CultureInfo.CurrentCulture );
      }
    }

    private void childLinkTextBox_TextChanged( object sender, EventArgs e ) {
      if ( !string.IsNullOrEmpty( childLinkTextBox.Text ) && childLinkTextBox.Text != "0" ) {
        linkButton.Image = Properties.Resources.Link16;
      }
    }

    private void childDaysTextBox_KeyPress( object sender, KeyPressEventArgs e ) {
      if ( !char.IsNumber( e.KeyChar ) && e.KeyChar != ( char ) Keys.Back
        && e.KeyChar != ( char ) Keys.Enter && e.KeyChar != ( char ) Keys.Tab ) {
        e.Handled = true;
      }
    }

    private void basicPriceTextBox_KeyPress( object sender, KeyPressEventArgs e ) {
      if ( !char.IsNumber( e.KeyChar ) && e.KeyChar != ( char ) Keys.Back && e.KeyChar != '.'
        && e.KeyChar != ( char ) Keys.Enter && e.KeyChar != ( char ) Keys.Tab && e.KeyChar != ',' ) {
        e.Handled = true;
      }
    }

    private void selectText_MouseDown( object sender, MouseEventArgs e ) {
      ( ( TextBox ) sender ).SelectAll( );
    }

    private void maskedTextBox_MouseDown( object sender, MouseEventArgs e ) {
      ( ( MaskedTextBox ) sender ).SelectAll( );
    }

    #endregion

    #region - Methods -

    private void SetDataBinding( ) {

      this.childNameTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childFirstNameTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "FirstName", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childNickNameTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "NickName", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childLinkTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Link", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childDateInPicker.DataBindings.Add( new Binding( "Value", this.controlBindingSource, "DateIn", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childDateOutPicker.DataBindings.Add( new Binding( "Value", this.controlBindingSource, "DateOut", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childBirthdayPicker.DataBindings.Add( new Binding( "Value", this.controlBindingSource, "Birthday", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childMinDaysTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "MinDays", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childMaxDaysTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "MaxDays", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childMaxTimeTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "MaxTime", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childMinTimeTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "MinTime", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.childPhotoPictureBox.DataBindings.Add( new Binding( "Image", this.controlBindingSource, "Photo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.remarksTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Remarks", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.basicPriceTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Mother.BasicPrice", true, DataSourceUpdateMode.OnPropertyChanged ) );

      this.week1MoFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1MoFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1MoToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1MoTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1TuFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1TuFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1TuToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1TuTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1WeFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1WeFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1WeToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1WeTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1ThFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1ThFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1ThToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1ThTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1FrFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1FrFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week1FrToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week1FrTo", true, DataSourceUpdateMode.OnPropertyChanged ) );

      this.week2MoFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2MoFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2MoToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2MoTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2TuFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2TuFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2TuToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2TuTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2WeFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2WeFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2WeToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2WeTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2ThFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2ThFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2ThToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2ThTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2FrFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2FrFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week2FrToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week2FrTo", true, DataSourceUpdateMode.OnPropertyChanged ) );

      this.week3MoFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3MoFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3MoToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3MoTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3TuFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3TuFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3TuToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3TuTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3WeFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3WeFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3WeToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3WeTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3ThFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3ThFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3ThToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3ThTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3FrFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3FrFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week3FrToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week3FrTo", true, DataSourceUpdateMode.OnPropertyChanged ) );

      this.week4MoFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4MoFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4MoToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4MoTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4TuFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4TuFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4TuToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4TuTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4WeFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4WeFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4WeToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4WeTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4ThFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4ThFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4ThToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4ThTo", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4FrFromTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4FrFrom", true, DataSourceUpdateMode.OnPropertyChanged ) );
      this.week4FrToTextBox.DataBindings.Add( new Binding( "Text", this.controlBindingSource, "Schedule.Week4FrTo", true, DataSourceUpdateMode.OnPropertyChanged ) );

    }

    private void ShowEditTrustPersons( ) {
      if ( trustPersonGridView.RowCount > 0 ) {

        TrustPersonForm trustPersonForm = new TrustPersonForm( currentChild.TrustPersons, trustPersonGridView.SelectedRows[ 0 ].Index );
        trustPersonForm.MdiParent = this.ParentForm.MdiParent;
        trustPersonForm.Show( );
      }
    }

    public void SetScheduleTypeView( Schedule.ScheduleType type ) {
      switch ( type ) {
        case Schedule.ScheduleType.Weekly:
          week1GroupBox.Visible = true;
          week2GroupBox.Visible = false;
          week3GroupBox.Visible = false;
          week4GroupBox.Visible = false;
          break;
        case Schedule.ScheduleType.Twoweekly:
          week1GroupBox.Visible = true;
          week2GroupBox.Visible = true;
          week3GroupBox.Visible = false;
          week4GroupBox.Visible = false;
          break;
        case Schedule.ScheduleType.Monthly:
          week1GroupBox.Visible = true;
          week2GroupBox.Visible = true;
          week3GroupBox.Visible = true;
          week4GroupBox.Visible = true;
          break;
        case Schedule.ScheduleType.Variable:
          week1GroupBox.Visible = false;
          week2GroupBox.Visible = false;
          week3GroupBox.Visible = false;
          week4GroupBox.Visible = false;
          break;
        default:
          break;
      }
    }

    private bool ScheduleIsEmpty( GroupBox weekGroupBox ) {
      bool isEmpty = true;
      foreach ( Control item in weekGroupBox.Controls ) {
        if ( item.GetType( ).Equals( typeof( MaskedTextBox ) ) ) {
          string text = ( ( MaskedTextBox ) item ).Text.Trim( );
          if ( !string.IsNullOrEmpty( text ) || text != ":" ) {
            isEmpty = false;
            break;
          }
        }
      }
      return isEmpty;
    }

    #endregion

    #region - Validation -

    private void childNameTextBox_Validating( object sender, CancelEventArgs e ) {
      childficheErrorProvider.SetError( childNameTextBox, "" );
      if ( string.IsNullOrEmpty( childNameTextBox.Text ) ) {
        childficheErrorProvider.SetError( childNameTextBox, "Naam verplicht." );
        e.Cancel = true;
      }
    }

    private void childDateInPicker_Validating( object sender, CancelEventArgs e ) {
      childficheErrorProvider.SetError( childDateInPicker, "" );
      if ( !childDateInPicker.Value.HasValue ) {
        childficheErrorProvider.SetError( childDateInPicker, "Datum inschrijving verplicht." );
        e.Cancel = true;
      }
    }

    private void childMinDaysTextBox_Validating( object sender, CancelEventArgs e ) {
      childficheErrorProvider.SetError( childMinDaysTextBox, "" );
      if ( !string.IsNullOrEmpty( childMinDaysTextBox.Text ) ) {
        if ( Convert.ToInt32( childMinDaysTextBox.Text, CultureInfo.CurrentCulture ) > 31 ) {
          childficheErrorProvider.SetError( childMinDaysTextBox, "Aantal te groot." );
          e.Cancel = true;
        }
        if ( Convert.ToInt32( childMinDaysTextBox.Text, CultureInfo.CurrentCulture ) < 1 ) {
          childficheErrorProvider.SetError( childMinDaysTextBox, "Aantal te klein." );
          e.Cancel = true;
        }
      }
    }

    private void childMaxDaysTextBox_Validating( object sender, CancelEventArgs e ) {
      childficheErrorProvider.SetError( childMaxDaysTextBox, "" );
      if ( !string.IsNullOrEmpty( childMaxDaysTextBox.Text ) ) {
        if ( Convert.ToInt32( childMaxDaysTextBox.Text, CultureInfo.CurrentCulture ) > 31 ) {
          childficheErrorProvider.SetError( childMaxDaysTextBox, "Aantal te groot." );
          e.Cancel = true;
        }
        if ( Convert.ToInt32( childMaxDaysTextBox.Text, CultureInfo.CurrentCulture ) < 1 ) {
          childficheErrorProvider.SetError( childMaxDaysTextBox, "Aantal te klein." );
          e.Cancel = true;
        }
      }
    }

    private void childMinTimeTextBox_Validating( object sender, CancelEventArgs e ) {
      childficheErrorProvider.SetError( childMinTimeTextBox, "" );
      if ( !string.IsNullOrEmpty( childMinTimeTextBox.Text ) && childMinTimeTextBox.Text.Trim( ) != ":" ) {
        Regex regex = new Regex( "^([0-1][0-9]|[2][0-3]):([0-5][0-9])$" );
        if ( !regex.IsMatch( childMinTimeTextBox.Text ) ) {
          childficheErrorProvider.SetError( childMinTimeTextBox, "Ongeldige tijd." );
          e.Cancel = true;
        }
      }
    }

    private void childMaxTimeTextBox_Validating( object sender, CancelEventArgs e ) {
      childficheErrorProvider.SetError( childMaxTimeTextBox, "" );
      if ( !string.IsNullOrEmpty( childMaxTimeTextBox.Text ) && childMaxTimeTextBox.Text.Trim( ) != ":" ) {
        Regex regex = new Regex( "^([0-1][0-9]|[2][0-3]):([0-5][0-9])$" );
        if ( !regex.IsMatch( childMaxTimeTextBox.Text ) ) {
          childficheErrorProvider.SetError( childMaxTimeTextBox, "Ongeldige tijd." );
          e.Cancel = true;
        }
      }

      //childficheErrorProvider.SetError( fromControl, "" );
      if ( ( string.IsNullOrEmpty( childMinTimeTextBox.Text ) || childMinTimeTextBox.Text.Trim( ) == ":" )
        && !( string.IsNullOrEmpty( childMaxTimeTextBox.Text ) || childMaxTimeTextBox.Text.Trim( ) == ":" ) ) {
        childficheErrorProvider.SetError( childMinTimeTextBox, "Start tijd verplicht." );
        e.Cancel = true;
      } else if ( !( string.IsNullOrEmpty( childMinTimeTextBox.Text ) || childMinTimeTextBox.Text.Trim( ) == ":" )
        && ( string.IsNullOrEmpty( childMaxTimeTextBox.Text ) || childMaxTimeTextBox.Text.Trim( ) == ":" ) ) {
        childficheErrorProvider.SetError( childMaxTimeTextBox, "Tot tijd verplicht." );
        e.Cancel = true;
      } else if ( !( string.IsNullOrEmpty( childMinTimeTextBox.Text ) || childMinTimeTextBox.Text.Trim( ) == ":" )
        && !( string.IsNullOrEmpty( childMaxTimeTextBox.Text ) || childMaxTimeTextBox.Text.Trim( ) == ":" ) ) {
        Regex regex = new Regex( "^([0-1][0-9]|[2][0-3]):([0-5][0-9])$" );
        if ( regex.IsMatch( childMinTimeTextBox.Text ) && regex.IsMatch( childMaxTimeTextBox.Text ) ) {
          TimeSpan fromTime = TimeSpan.Parse( childMinTimeTextBox.Text );
          TimeSpan toTime = TimeSpan.Parse( childMaxTimeTextBox.Text );
          if ( toTime < fromTime ) {
            childficheErrorProvider.SetError( childMaxTimeTextBox, "Tot tijd moet groter dan Start tijd zijn." );
            e.Cancel = true;
          }
        }
      }
    }

    private void basicPriceTextBox_Validating( object sender, CancelEventArgs e ) {
      childficheErrorProvider.SetError( basicPriceTextBox, "" );

      if ( string.IsNullOrEmpty( basicPriceTextBox.Text ) || Convert.ToDecimal( basicPriceTextBox.Text ) == 0m ) {
        childficheErrorProvider.SetError( basicPriceTextBox, "Prijs is verplicht." );
        e.Cancel = true;
      }

    }

    private void weekFromTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateTimeBox( ( MaskedTextBox ) sender, e );
    }

    private void week1MoToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week1MoFromTextBox, week1MoToTextBox, e );
    }

    private void week1TuToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week1TuFromTextBox, week1TuToTextBox, e );
    }

    private void week1WeToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week1WeFromTextBox, week1WeToTextBox, e );
    }

    private void week1ThToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week1ThFromTextBox, week1ThToTextBox, e );
    }

    private void week1FrToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week1FrFromTextBox, week1FrToTextBox, e );
    }

    private void week2MoToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week2MoFromTextBox, week2MoToTextBox, e );
    }

    private void week2TuToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week2TuFromTextBox, week2TuToTextBox, e );
    }

    private void week2WeToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week2WeFromTextBox, week2WeToTextBox, e );
    }

    private void week2ThToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week2ThFromTextBox, week2ThToTextBox, e );
    }

    private void week2FrToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week2FrFromTextBox, week2FrToTextBox, e );
    }

    private void week3MoToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week3MoFromTextBox, week3MoToTextBox, e );
    }

    private void week3TuToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week3TuFromTextBox, week3TuToTextBox, e );
    }

    private void week3WeToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week3WeFromTextBox, week3WeToTextBox, e );
    }

    private void week3ThToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week3ThFromTextBox, week3ThToTextBox, e );
    }

    private void week3FrToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week3FrFromTextBox, week3FrToTextBox, e );
    }

    private void week4MoToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week4MoFromTextBox, week4MoToTextBox, e );
    }

    private void week4TuToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week4TuFromTextBox, week4TuToTextBox, e );
    }

    private void week4WeToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week4WeFromTextBox, week4WeToTextBox, e );
    }

    private void week4ThToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week4ThFromTextBox, week4ThToTextBox, e );
    }

    private void week4FrToTextBox_Validating( object sender, CancelEventArgs e ) {
      ValidateToTimeBox( week4FrFromTextBox, week4FrToTextBox, e );
    }

    private void ValidateTimeBox( Control timeBoxControl, CancelEventArgs e ) {
      scheduleErrorProvider.SetError( timeBoxControl, "" );
      if ( !string.IsNullOrEmpty( timeBoxControl.Text ) && timeBoxControl.Text.Trim( ) != ":" ) {
        Regex regex = new Regex( "^([0-1][0-9]|[2][0-3]):([0-5][0-9])$" );
        if ( !regex.IsMatch( timeBoxControl.Text ) ) {
          scheduleErrorProvider.SetError( timeBoxControl, "Ongeldige tijd." );
          e.Cancel = true;
        }
      }
    }

    private void ValidateToTimeBox( Control fromControl, Control toControl, CancelEventArgs e ) {
      ValidateTimeBox( toControl, e );

      if ( ( string.IsNullOrEmpty( fromControl.Text ) || fromControl.Text.Trim( ) == ":" )
        && !( string.IsNullOrEmpty( toControl.Text ) || toControl.Text.Trim( ) == ":" ) ) {
        scheduleErrorProvider.SetError( fromControl, "Start tijd verplicht." );
        e.Cancel = true;
      } else if ( !( string.IsNullOrEmpty( fromControl.Text ) || fromControl.Text.Trim( ) == ":" )
        && ( string.IsNullOrEmpty( toControl.Text ) || toControl.Text.Trim( ) == ":" ) ) {
        scheduleErrorProvider.SetError( toControl, "Tot tijd verplicht." );
        e.Cancel = true;
      } else if ( !( string.IsNullOrEmpty( fromControl.Text ) || fromControl.Text.Trim( ) == ":" )
        && !( string.IsNullOrEmpty( toControl.Text ) || toControl.Text.Trim( ) == ":" ) ) {
        Regex regex = new Regex( "^([0-1][0-9]|[2][0-3]):([0-5][0-9])$" );
        if ( regex.IsMatch( fromControl.Text ) && regex.IsMatch( toControl.Text ) ) {
          TimeSpan fromTime = TimeSpan.Parse( fromControl.Text );
          TimeSpan toTime = TimeSpan.Parse( toControl.Text );
          if ( toTime < fromTime ) {
            scheduleErrorProvider.SetError( toControl, "Tot tijd moet groter dan Start tijd zijn." );
            e.Cancel = true;
          }
        }
      }
    }

    private void radioButton_Validating( object sender, CancelEventArgs e ) {
      if ( weekRadioButton.Checked ) {

        if ( ScheduleIsEmpty( week1GroupBox ) ) {
          childficheErrorProvider.SetError( weekRadioButton, "Weekelijks schema niet ingevuld." );
          e.Cancel = true;
        }
        currentChild.Schedule.ClearWeek2( );
        currentChild.Schedule.ClearWeek3( );
        currentChild.Schedule.ClearWeek4( );
      } else if ( twoWeekRadioButton.Checked ) {
        if ( ScheduleIsEmpty( week1GroupBox ) && ScheduleIsEmpty( week2GroupBox ) ) {
          childficheErrorProvider.SetError( twoWeekRadioButton, "2-weekelijks schema niet ingevuld." );
          e.Cancel = true;
        }
        currentChild.Schedule.ClearWeek3( );
        currentChild.Schedule.ClearWeek4( );
      } else if ( monthRadioButton.Checked ) {
        if ( ScheduleIsEmpty( week1GroupBox ) && ScheduleIsEmpty( week2GroupBox )
          && ScheduleIsEmpty( week3GroupBox ) && ScheduleIsEmpty( week4GroupBox ) ) {
          childficheErrorProvider.SetError( monthRadioButton, "2-weekelijks schema niet ingevuld." );
          e.Cancel = true;
        }
      } else if ( variableRadioButton.Checked ) {
        currentChild.Schedule.Clear( );
      } else {
        variableRadioButton.Checked = true;
        currentChild.Schedule.Clear( );
      }
    }

    private void control_Validated( object sender, EventArgs e ) {
      childficheErrorProvider.SetError( ( Control ) sender, "" );
    }

    #endregion

    private void basicPriceTextBox_TextChanged( object sender, EventArgs e ) {
      System.Diagnostics.Debug.WriteLine( basicPriceTextBox.Text );
    }

  }
}
